layui.use(['element','form','table','laydate'],function(){
    var table = layui.table;
    var form = layui.form;

    var clientName = '';

    table.render({
        elem: '#demo'
        ,toolbar: '#toolbarDemo'
        ,url: '/copClient' //数据接口
        ,page: true //开启分页
        ,where:{
            clientName
        }
        ,cols: [[ //表头
            {field: 'clientId',title: '客户编号', sort: true, width:110}
            ,{field: 'clientName', edit: 'text',title: '客户姓名', width:100}
            ,{field: 'clientGender', edit: 'text',title: '客户性别', width:100}
            ,{field: 'clientBirthday',title: '生日', width:120}
            ,{field: 'clientAge', title: '年龄', width:100}
            ,{field: 'clientPhone', edit: 'text',title: '电话'}
            ,{field: 'clientEmail', edit: 'text',title: '邮箱'}
            ,{field: 'clientAddress', edit: 'text',title: '地址'}
            ,{field: 'clientMaritalStatus',title: '婚姻情况'}
            ,{field: 'clientIdCard',title: '身份证号'}
            ,{fixed: 'right',align:'center', toolbar: '#barDemo',title: '操作', width:120}
        ]]
        ,id :'client'
    });

    //工具栏监听事件--删除
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var id = obj.data.clientId;
        if(layEvent === 'del'){
            layer.confirm('真的删除行么', function(index){
                obj.del();
                layer.close(index);
                console.log(data)
                //向服务端发送删除指令
                $.ajax({
                    url: '/copClient/'+id,
                    type: 'DELETE',
                    dataType: 'json',
                    error: function (xhr, ajaxOptions, thrownError) {
                        if(xhr.status==403) {
                            alert("无权限")
                            location.reload([true])
                        }
                    }
                })
                    .done(function (result) {
                        if (result.code == 500) {
                            alert("修改失败")
                            location.reload([true])
                        }else{
                            alert("修改成功")
                        }
                    });
            });
        }else if (layEvent === 'edit'){
            window.location.href="clientDetail.html?#"+id
        }
    });

    //监听单元格编辑--编辑
    table.on('edit(test)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        var a = {
            'clientId': data.clientId,
            'clientName': data.clientName,
            'clientGender': data.clientGender,
            'clientPhone':data.clientPhone,
            'clientEmail':data.clientEmail,
            'clientAddress':data.clientAddress,
            'clientMaritalStatus':data.clientMaritalStatus
        };
        var data = JSON.stringify(a)
        $.ajax({
            url: '/copClient',
            type: 'PUT',
            headers: { 'Content-Type': 'application/json;charset=utf-8' },
            dataType: 'json',
            data: data,
            error: function (xhr, ajaxOptions, thrownError) {
                if(xhr.status==403) {
                    alert("无权限")
                    location.reload([true])
                }
            }
        })
            .done(function (result) {
                if (result.code == 500) {
                    layer.msg('修改失败', {
                        icon: 1
                    })
                    location.reload([true])
                }else{
                    alert("修改成功")
                    location.reload([true])
                }
            });
    });

    //头部工具栏监听事件--增加
    table.on('toolbar(test)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                window.location.href="clientAdd.html"
                break;
        };
    });

    var $ = layui.$, active = {
        reload: function(){
            var clientName = document.getElementById('clientName').value;

            var table = layui.table;
            table.reload('client', {
                where: {
                    clientName
                }
            });
        }
    };

    $('.searchTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

});